/**
 * @file protocol_params.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#include "aadl/flash.h"
#include "flash/flash_main.h"
#include "protocol_params.h"

#include "global.h"

#pragma section = "__section_params_update"
#define params_cb_start __section_begin("__section_params_update")
#define params_cb_end __section_end("__section_params_update")

#pragma section = "__section_params_init"
#define params_init_start __section_begin("__section_params_init")
#define params_init_end __section_end("__section_params_init")

#include "service/include/swei_config.h"
/*配置参数id表 --------------------145 - 62 =  83 -------------------------------*/
struct flash_data config_params_table[CONFIG_ID_TOTAL] =
{
        {CONFIG_ID_SN,   sizeof(char[SN_MAX_SIZE])},
        {CONFIG_ID_APN,  sizeof(apn_param_t)},
        {CONFIG_ID_LOG,  sizeof(log_params_t)},
        {CONFIG_ID_PLAT, sizeof(platform_config_param_st)},
        {CONFIG_ID_MAIN_IP, sizeof(server_params_t)},
        {CONFIG_ID_TEST_IP, sizeof(server_params_t)},
};

/*******************************************************************************
 * @brief	   协议参数初始化
 * @return 	   none
 *******************************************************************************/
void config_params_init(void)
{
    params_init_manager_t *p;

    for (p = (params_init_manager_t *)params_init_start;
         p < (params_init_manager_t *)params_init_end; p++)
    {
        if (p->init_handler != NULL)
        {
            p->init_handler();
        }
    }
}

/*******************************************************************************
 * @brief	   参数更新处理入口
 * @param[in]   id       - 配置参数id
 * @param[in]   new_conf - 最新的配置参数
 * @param[in]   old_conf - 之前的配置参数
 * @return 	   none
 *******************************************************************************/
void config_update_with_callback(config_id id, const void *new_conf,
                                    const void *old_conf)
{
    params_update_manager_t *p;
    for (p = (params_update_manager_t *)params_cb_start;
         p < (params_update_manager_t *)params_cb_end; p++)
    {
        if (id >= p->start && id <= p->end && p->callback != NULL)
            p->callback(new_conf, old_conf); /*参数更新通知上层,同步更新关键参数 */
    }

    config_set(id, new_conf); /*保存最新参数 -----*/
}

#ifdef TPMS_ENABLE
/*******************************************************************************
 * @brief	   参数更新处理入口
 * @param[in]   id       - 配置参数id
 * @param[in]   new_conf - 最新的配置参数
 * @param[in]   old_conf - 之前的配置参数
 * @return 	   none
 *******************************************************************************/
void config_update_with_callback_extend(config_id3 id, const void *new_conf,
                                           const void *old_conf)
{
    params_update_manager_t *p;
    for (p = (params_update_manager_t *)params_cb_start;
         p < (params_update_manager_t *)params_cb_end; p++)
    {
        if (id >= p->start && id <= p->end && p->callback != NULL)
            p->callback(new_conf, old_conf); /*参数更新通知上层 */
    }
    protocol_set_conf_extend(id, new_conf); /*保存最新参数 -----*/
}
#endif

uint32_t protocol_fatal_backup = 0;

/* 写关键参数备份区 */
int32_t config_backup_set(config_id id, const void *conf)
{
    return aad_flash_zone_write(FATAL_PARA_ID2, (int32_t)id, (unsigned char *)conf, 0, 0) >= 0;
}

/* 读关键参数备份区 */
int32_t config_backup_get(config_id id, void *conf)
{
    return aad_flash_zone_read(FATAL_PARA_ID2, (int32_t)id, (unsigned char *)conf) > 0;
}

/*******************************************************************************
 * @brief	   设置配置参数
 * @param[in]   id   - 配置参数id
 * @param[in]   conf - 参数值
 * @return 	   0 - 设置失败,非0 - 设置成功
 *******************************************************************************/
#pragma optimize = none
int32_t config_set(config_id id, const void *conf)
{
#if FATAL_PARAM_BACKUP_USE
    /* 关键参数备份区 */
    if (1 == protocol_fatal_backup)
        config_backup_set(id, conf);
#endif

    return aad_flash_zone_write(FATAL_PARA_ID, (int32_t)id, (unsigned char *)conf, 0, 0) >= 0;
}

/*******************************************************************************
 * @brief	   读取配置参数
 * @param[in]   id   - 配置参数id
 * @param[out]  conf - 指向接收参数值的指针
 * @return 	   0 - 读取失败,非0 - 读取成功
 *******************************************************************************/
#pragma optimize = none
int32_t config_get(config_id id, void *conf)
{
    return aad_flash_zone_read(FATAL_PARA_ID, (int32_t)id, (unsigned char *)conf) > 0;
}
